var http = require('http'),
	fs = require('fs');
var flvUrl = 'http://f.youku.com/player/getFlvPath/sid/00_00/st/flv/fileid/030002010050F8CC304D2407A9ABC0B8896CD8-89FC-19E7-98E6-BCCFD6FF65A2?K=236086b96d2660862828858f';
var filepath = '/tmp/' + new Date().getTime() + '.flv';
var writableStream = fs.createWriteStream(filepath);

function getHttpContent(url, callback){
	http.get(url, function(res){
		var statusCode = res.statusCode;
		if(statusCode == 200){
			res.on('data', callback.data);
			res.on('end', callback.end);
		}else if(statusCode == 302){
			var headers = res.headers;
			var location = headers['location'];
			getHttpContent(location, callback);
		}
	})
}
var dataCallback = function(chunk){
//	fs.appendFile(filepath, chunk, function(err){
//		if(err){
////			console.log(err)
//			throw err;
//		
//		}
//	})
	writableStream.write(chunk)
};

var endCallback = function(err){
	console.log(filepath + ' end')
	writableStream.close()
}

getHttpContent(flvUrl, {
	'data'	:	dataCallback,
	'end'	:	endCallback
})